package ttOrder.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ttOrder.dao.MycartMapper;
import ttOrder.dao.OrderMapper;
import ttOrder.model.Food;
import ttOrder.model.Order;
import ttOrder.model.OrderDetall;
import ttOrder.vo.FoodVO;
import ttOrder.vo.OrderVO;

@Service
public class MycartService implements IMycartService{
	@Autowired
	MycartMapper mycartMapper;
	
	
	@Autowired
	OrderMapper orderMapper;
	
	private float allPrice=0.0f;
	public OrderVO getAllPrice(){
		OrderVO vo = new OrderVO();
		vo.setTotalprice(this.allPrice);
		return vo;
	}
	
	//定义一个hashMap集合,用于存放菜品的id和菜品数量
	HashMap<Integer,String> hm = new HashMap<Integer,String>();
	
	//1.添加菜品
	public void addFood(Integer foodId,String foodNum){
		hm.put(foodId, foodNum);
	}
	//2.删除菜品
	public void deleteFood(Integer foodId){
		hm.remove(foodId);
	}
	//3.清空菜品
	public void clear(){
		hm.clear();
	}
	//4.修改菜品数量
	public void upFood(Integer foodId,String newNum){
		hm.put(foodId, newNum);
	}
	
	//5.显示购物车
	public List<FoodVO> showMyCart(){
		List<FoodVO> alvo = new ArrayList<FoodVO>();
		List<Food> al = new ArrayList<Food>();
		//使用迭代器完成从hm中取出菜品id的要求
		Iterator<Integer> it = hm.keySet().iterator();
		String nul="(null)";
		String sub="(";
		while(it.hasNext()){
			
			//取出foodId
			Integer foodId = (Integer) it.next();
	
				if(it.hasNext()){
					sub+=foodId+",";
				}else{
					sub+=foodId+")";
				}
					
		}
		if(sub == "("){
			al = mycartMapper.select(nul);
		}else{
			al = mycartMapper.select(sub);
		}
		
		this.allPrice = 0.0f;
		for(Food f:al){
			FoodVO foodVO = new FoodVO();
			BeanUtils.copyProperties(f, foodVO);
			float unit =  f.getPrice();
			this.allPrice = this.allPrice+unit*Integer.parseInt(this.getFoodsNumById(f.getFoodId()));
			foodVO.setFoodCount(hm.get(f.getFoodId()));
			alvo.add(foodVO);
		}
		
		return alvo;
	

	}
	//更具id返回数量
	@Override
	public String getFoodsNumById(Integer foodId) {
		return hm.get(foodId);
	}
	

	//购物车提交订单(前台)
	@Override
	public void insertOrderDetall() {
		List<FoodVO> listVO = showMyCart();
		for(FoodVO vo:listVO){
		
			OrderDetall orderDetall = new OrderDetall();
			Integer foodcount = Integer.parseInt(hm.get(vo.getFoodId()));
			orderDetall.setFoodcount(foodcount);
			orderDetall.setOrderId(orderMapper.getOrderId());
			orderDetall.setFoodId(vo.getFoodId());
			orderMapper.insertOrderDetall(orderDetall);
		}
	}
	//更改订单的另外两个属性
	@Override
	public void editOrder() {
		Order order = new Order();
		OrderVO vo = new OrderVO();
		vo = getAllPrice();	
		java.util.Date date=new java.util.Date();
		java.sql.Timestamp sqldate=new java.sql.Timestamp(date.getTime());		
		vo.setOrderdate(sqldate);
		vo.setOrderId(orderMapper.getOrderId());
		BeanUtils.copyProperties(vo, order);
		orderMapper.editOrder(order);
	}
}
